// pages/choujiang/choujiang.js
//计数器
var interval = null;
//值越大旋转时间越长  即旋转速度
var intime = 50;
var uid = ''
var uname=''
var uface=''
var upoints=''
var  inumber =3
var  random  =0
var pr = new Array;
var con='未中奖'
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    color: [0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7],
    //9张奖品图片
    images: [
      'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/book_image/book1.png', 
      'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/xiexiecanyu.png', 
      'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/book_image/book3.png', 
      'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/xiexiecanyu.png', 
      'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/book_image/book5.png', 
      'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/xiexiecanyu.png', 
      'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/book_image/book7.png', 
      'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/xiexiecanyu.png', 
  ],

    number:3,
    //确定按钮
    btnconfirm: 'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/dianjichoujiang.png',
    //点击事件
    clickLuck:'clickLuck',


  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
    this.loadAnimation();
    var that =this;
    wx.request({
      url: app.globalData.url + 'v1/user/'+app.globalData.userInfo.id,  //********id查询************* */
      method: 'GET',
      success (res) {
        console.log(res)
        that.setData({
          id: res.data.user.id,
          name: res.data.user.name,
          face: res.data.user.face,
          points: res.data.user.points
          //*********************
        })
        uid=that.data.id,
        uname = that.data.name,
        uface = that.data.face,
        upoints = that.data.points

      }
    })
  },

prize:function(){
  for(var i =0; i<pr.length; i++){
    var a= pr[i]
    if(a==0 ||a==2||a==4||a==6){
      con=''
       break
    }
  }
  for(var i =0; i<pr.length; i++){
    var a= pr[i]
    if(a==0){
      con+= ' '+'旅游天地'+' '
    }
    if(a==2){
      con+= ' '+'奥秘'+' '
    }
    if(a==4){
      con+= ' '+'环球人物'+' '
    }
    if(a==6){
      con+= ' '+'新周刊'+' '
    }
  }
  wx.showModal({
    title: '我的奖品',
    content: con,
    showCancel:false,
    success (res) {
      if (res.confirm) {
        console.log('用户点击确定')
      } 
    }
  })

},

  //点击抽奖按钮
  clickLuck:function(){
    random= Math.floor(Math.random()*8)
    pr.push(random)
    console.log(pr)
    var e = this;
    wx.setStorage({
      key:'number',
      data:this.data.number-1
    })
    upoints=upoints-1
    console.log(this.data.points)
    console.log(upoints)
    if(this.data.number>0 && upoints>=0){
        e.setData({
              number:e.data.number-1
            })
            //更新积分
            wx.request({
              url:  app.globalData.url + 'v1/user/update',
              method: 'POST',
              data: {
                id:uid,
                name:uname,
                face:uface,
                points:upoints
              },    
              success (res) {
                console.log(res)
              }
            })
    }else if(upoints<0){
      wx.showToast({
        title: '您的积分不足！',
        icon: 'none',
        duration: 1500
      })
      return;
    }else{
      // 设置按钮不可点击
      e.setData({
        btnconfirm:'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/dianjichoujiang.png',
        clickLuck:'clickLuck',
      })
      wx.showModal({
        title: '提示',
        content: '抽奖次数不足',
        showCancel: false,
   
      })
      return;
    }
   
 
    // 设置按钮不可点击
    e.setData({
      btnconfirm:'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/dianjichoujiang.png',
      clickLuck:'',
    })
    //清空计时器
    clearInterval(interval);
    var index = 0;
    console.log(e.data.color[0]);
    //循环设置每一项的透明度
    interval = setInterval(function () {
      if (index > 7) {
        index = 0;
        e.data.color[7] = 0.5
      } else if (index != 0) {
        e.data.color[index - 1] = 0.5
      }
      e.data.color[index] = 1
      e.setData({
        color: e.data.color,
      })
      index++;
    }, intime);

    //模拟网络请求时间  设为两秒
    var stoptime = 2000;
    setTimeout(function () {
      e.stop(e.data.luckPosition);
    }, stoptime)

  },

  stop: function (which){
    var e = this;
    //清空计数器
    clearInterval(interval);
    //初始化当前位置
    var current = -1;
    var color = e.data.color;
    for (var i = 0; i < color.length; i++) {
      if (color[i] == 1) {
        current = i;
      }
    }
    //下标从1开始
    var index = current + 1;
    e.stopLuck(which, index, intime, 10);
  },


/**
 * which:中奖位置
 * index:当前位置
 * time：时间标记
 * splittime：每次增加的时间 值越大减速越快
 */
  stopLuck: function (which, index,time,splittime){
    var e = this;
    //值越大出现中奖结果后减速时间越长
    var color = e.data.color;
    setTimeout(function () {
      //重置前一个位置
      if (index > 7) {
        index = 0;
        color[7] = 0.5
      } else if (index != 0) {
        color[index - 1] = 0.5
      }
      //当前位置为选中状态
      color[index] = 1
      e.setData({
        color: color,
      })
          //如果旋转时间过短或者当前位置不等于中奖位置则递归执行
          //直到旋转至中奖位置

        which = random
        if (time < 400 || index != which){
          // if (time < 400+ Math.floor(Math.random()*200) ){
          //越来越慢
          splittime++;
          time += splittime;
          //当前位置+1
          index++;
          e.stopLuck(which, index, time, splittime);
        }else{

        //1秒后显示弹窗
          setTimeout(function () {
        // if (which == 1 || which == 3 || which == 5 || which == 7) {
          if (which == index && (which==0 ||which==2||which==4||which==6)) {
            //中奖
            wx.showModal({
              title: '提示',
              content: '恭喜中奖',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  //设置按钮可以点击
                  e.setData({
                    btnconfirm: 'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/dianjichoujiang.png',
                    clickLuck: 'clickLuck',
                  })
                  e.loadAnimation();
                }
              }
            })
          } else {
            //未中奖
            wx.showModal({
             title: '提示',
              content: '很遗憾未中奖',
              showCancel: false,
              success:function(res){
                if(res.confirm){
                  //设置按钮可以点击
                  e.setData({
                    btnconfirm: 'https://sign-face.oss-cn-shanghai.aliyuncs.com/img/dianjichoujiang.png',
                    clickLuck: 'clickLuck',
                  })
                  e.loadAnimation();
                }
              }
            })
          }
          }, 1000);
        }
    }, time);
    // console.log(time);
  },
  //进入页面时缓慢切换
 loadAnimation:function (){
  var e = this;
  var index = 0;
  // if (interval == null){
  interval = setInterval(function () {
    if (index > 7) {
      index = 0;
      e.data.color[7] = 0.5
    } else if (index != 0) {
      e.data.color[index - 1] = 0.5
    }
    e.data.color[index] = 1
    e.setData({
      color: e.data.color,
    })
    index++;
  }, 1000);
  // }  
},
 
goHome: function(e) {
  wx.switchTab({
    url: '../jifen/jifen'
  })
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that =this;
    wx.getStorage({//获取本地缓存
      key:"number",
      success:function(res){
        console.log(res)
        if(res.data!=undefined&&res.data>0){
          that.setData({
            number:res.data
          });
          // console.log(that.data.number)
        }else if(res.data!=undefined&&res.data<=0){
          that.setData({
            number:0
          });
        }else{
          that.setData({
            number:that.data.number
          })
          // console.log(that.data.number+'1324')
        }
      },
    })
   

  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})